package com.huan.lossless;

import com.alibaba.cloud.nacos.NacosConfigManager;
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import com.alibaba.cloud.nacos.NacosServiceManager;
import com.alibaba.nacos.api.exception.NacosException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 触发服务下线
 *
 * @author huan.fu
 * @date 2023/3/8 - 08:51
 */
@Component
@Slf4j
public class LosslessOfflineApi {

    @Resource
    private NacosConfigManager nacosConfigManager;
    @Resource
    private NacosServiceManager nacosServiceManager;
    @Resource
    private NacosDiscoveryProperties nacosDiscoveryProperties;

    /**
     * 服务下线
     *
     * @throws NacosException NacosException
     */
    public void offlineService() throws NacosException {
        log.info("触发服务下线 serviceName:[{}]", nacosDiscoveryProperties.getService());
        nacosServiceManager.nacosServiceShutDown();
        nacosConfigManager.getConfigService()
                .publishConfig(NacosConstant.DATA_ID, NacosConstant.GROUP,
                        nacosDiscoveryProperties.getService() + NacosConstant.SPLIT + System.currentTimeMillis());
    }
}
